package com.hns.phone.pushMsg.util;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hns.phone.pushMsg.vo.PushMessageParamsVo;
import com.hns.tool.pub.DateTool;
import com.hns.tool.pub.PubUtil;

/**
 * 安卓组播
 * @author:zhangyu
 * @email:zhangyu2222z@sina.cn
 * @version Revision 2.0.0
 */
@Component
public class AndGroupNotification extends AndroidNotification {
	
	@Autowired
	private BasePushClient client;
	
	/**
	 * 过滤条件
	 * @param filter
	 * @throws Exception
	 * @author:zhangyu
	 * @email:zhangyu2222z@sina.cn
	 */
	private void setFilter(JSONObject filter) throws Exception {
		setPredefinedKeyValue("filter", filter);
	}

	/**
	 * 发送信息动作
	 * @param params
	 * @throws Exception
	 * @see com.hns.can.pushMsg.util.AndroidNotification#sendMsg(com.hns.can.pushMsg.vo.PushMessageParamsVo)
	 * @author:zhangyu
	 * @email:zhangyu2222z@sina.cn
	 */
	@Override
	public String sendMsg(PushMessageParamsVo params) throws Exception {
		setPredefinedKeyValue("appkey", params.getAppkey());
		setAppMasterSecret(params.getAppMasterSecret());
		this.setPredefinedKeyValue("type", "groupcast");
		JSONObject filterJson = new JSONObject();
		JSONObject whereJson = new JSONObject();
		JSONArray tagArray = new JSONArray();
//		if("MDG00004".equals(params.getGroupType())){
//			for(String tag : params.getSelectedTags()){
//				JSONObject testTag = new JSONObject();
//				testTag.put("tag", tag);
//				tagArray.put(testTag);
//			}
//		}else{
//			JSONObject testTag = new JSONObject();
//			testTag.put("tag", params.getGroupType());
//			tagArray.put(testTag);
//		}
		JSONObject testTag = new JSONObject();
		testTag.put("tag", params.getGroupType());
		tagArray.put(testTag);
		
		
		whereJson.put("and", tagArray);
		filterJson.put("where", whereJson);
//		System.out.println(filterJson.toString());
		this.setFilter(filterJson);
		this.setTicker(params.getTitle());
		this.setTitle(params.getTitle());
		this.setText(params.getContent());
//		this.setExtraField("msgId", params.getMsgId());
		this.goAppAfterOpen();
//		if("MOW00001".equals(params.getPushType())){
//			this.goAppAfterOpen();
//		}else if("MOW00002".equals(params.getPushType())){
//			this.goUrlAfterOpen(params.getWebUrl());
//		}
		
		this.setDisplayType(params.getDisplayType());
		this.setProductionMode();
		
		this.setPlayLights(true);
		this.setPlayVibrate(true);
		this.setPlaySound(true);
		if(PubUtil.isNotEmpty(params.getDelayTime())){
			this.setStartTime(params.getDelayTime() + "00:00:00");
			String expireTime = DateTool.calculateByDate(params.getDelayTime(), 3);
			this.setExpireTime(expireTime + "23:59:59");
		}
		
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		this.setExtraField("crtTime", format.format(new Date()));
		
//		this.setTestMode();
		String taskId = client.sendMsg(this);
		return taskId;
	}
//	public void sendMsg(){}

}
